package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.iexbuy.ihg.R;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.entity.ShopCenter;
import com.iexbuy.ihg.entity.YunDealObject;
import com.iexbuy.ihg.entity.YunItemListObject;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.HashMap;

public class YunGuoDescFragment extends BaseFragment {
	
	private static YunGuoDescFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.webview)
	private WebView webview;

	private static YunItemListObject yunItemListObject;
	private static String webUrl;
	private static String shopName;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_yunguo_desc, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		if(shopName==null){
			mBaseActivity.setTitle(getResources().getString(R.string.yun_guo_image_detail));
		}else {
			mBaseActivity.setTitle(shopName);
		}

		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);
		if (yunItemListObject != null) {
			getWebViewData();
		}else if(webUrl!=null){
			initWebViewData(webUrl);
		}

	}

	private void getWebViewData() {
		if (yunItemListObject == null) return;

		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("itemId", yunItemListObject.getItemId());
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_CHOU_JIANG_ITEM_DETAIL, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
					}

					@Override
					public void onCancelled() {
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						yunItemListObject = JsonUtils.parseJsonStrToObject(JsonUtils.getJsonSubStringByKey(responseInfo.result, "body"), YunItemListObject.class);

						responseInfo.result = responseInfo.result.replaceAll("\\<img ", "\\<img width='100%'");
						initWebViewData(yunItemListObject.getUrl());
					}

					@Override
					public void onFailure(HttpException error, String msg) {
					}
				});
	}
	private void initWebViewData(String url){

		webview.getSettings().setDefaultTextEncodingName("UTF-8");
		webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
		webview.getSettings().setUseWideViewPort(true);
		webview.getSettings().setLoadWithOverviewMode(true);
		webview.getSettings().setBuiltInZoomControls(true);

		webview.loadData(url, "text/html; charset=UTF-8", null);
	}

	public static void setYunItemListObject(YunItemListObject yunItemListObject) {
		YunGuoDescFragment.yunItemListObject = yunItemListObject;
	}
	public static void setWebUrl(String webUrl) {
		YunGuoDescFragment.webUrl = webUrl;
	}
	public static void setShopName(String shopName) {
		YunGuoDescFragment.shopName = shopName;
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static YunGuoDescFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
